package com.litidea.dao.impl;

import static com.litidea.commons.dict.StaticDics.EVALUATION_RESULT;
import static com.litidea.commons.dict.StaticDics.EXTERNAL_CHANGE_TICKET_STATUS;
import static com.litidea.commons.dict.StaticDics.IMPACT_FOR_PRODUCE_HANDLE;
import static com.litidea.commons.dict.StaticDics.IMPACT_FOR_RELIABILITY;
import static com.litidea.commons.dict.StaticDics.OK_MAP;

import java.util.List;
import java.util.Map;

import net.sf.json.JSONObject;

import com.litidea.commons.utils.JSONUtils;
import com.litidea.dao.ExternalChangeDAO;
import com.litidea.dao.abs.ExternalChangeDAOAbs;
import com.litidea.dataobjects.ExternalChangeDO;

@SuppressWarnings("unchecked")
public class ExternalChangeDAOImpl extends ExternalChangeDAOAbs implements
		ExternalChangeDAO {

	/**
	 * 获取外部变更数量
	 * 
	 * @param params
	 * @return
	 */
	private Integer getExternalChangeCount(Map<String, String> params) {
		return (Integer) this.getSqlMapClientTemplate().queryForObject(
				"externalchange.getExternalChangeCount", params);
	}

	@Override
	public Integer addNewExternalChange(Map<String, String> params) {
		// TODO Auto-generated method stub
		Integer id = (Integer) this.getSqlMapClientTemplate().insert(
				"externalchange.addExternalChange", params);
		return id;
	}

	@Override
	public JSONObject getExternalChangePageList(Map<String, String> params) {
		// TODO Auto-generated method stub
		List<ExternalChangeDO> list = this.getSqlMapClientTemplate()
				.queryForList("externalchange.getExternalChanges", params);

		for (ExternalChangeDO externalChangeDO : list) {

			externalChangeDO
					.setEvaImpactForReliabilityString(IMPACT_FOR_RELIABILITY
							.get(String.valueOf(externalChangeDO
									.getEvaImpactForReliability())));
			externalChangeDO
					.setEvaImpactForProductHandleString(IMPACT_FOR_PRODUCE_HANDLE
							.get(String.valueOf(externalChangeDO
									.getEvaImpactForProductHandle())));
			externalChangeDO.setMustVerifyString(OK_MAP.get(externalChangeDO
					.getMustVerify()));
			externalChangeDO.setMustAlertCustomerString(OK_MAP
					.get(externalChangeDO.getMustAlertCustomer()));
			externalChangeDO.setMustSpplierChangeString(OK_MAP
					.get(externalChangeDO.getMustSpplierChange()));
			externalChangeDO.setResultString(EVALUATION_RESULT
					.get(externalChangeDO.getResult()));
			externalChangeDO.setSampleIsQualifiedString(OK_MAP
					.get(externalChangeDO.getSampleIsQualified()));
			externalChangeDO.setSmallIsQualifiedString(OK_MAP
					.get(externalChangeDO.getSmallIsQualified()));
			externalChangeDO.setMiddleIsQualifiedString(OK_MAP
					.get(externalChangeDO.getMiddleIsQualified()));
			externalChangeDO.setIsFinishedAlertCustomerString(OK_MAP
					.get(externalChangeDO.getIsFinishedAlertCustomer()));
			externalChangeDO.setIsFinishedSupChangeString(OK_MAP
					.get(externalChangeDO.getIsFinishedSupChange()));
			externalChangeDO.setIsFinishedSecurityString(OK_MAP
					.get(externalChangeDO.getIsFinishedSecurity()));
			externalChangeDO.setChangeResultIsQualifyString(OK_MAP
					.get(externalChangeDO.getChangeResultIsQualify()));
			externalChangeDO.setIsApproveChangeString(OK_MAP
					.get(externalChangeDO.getIsApproveChange()));

			externalChangeDO.setResearchDeptIsBomChangedString(OK_MAP
					.get(externalChangeDO.getResearchDeptIsBomChanged()));
			externalChangeDO.setResearchDeptIsDesignDocChangedString(OK_MAP
					.get(externalChangeDO.getResearchDeptIsDesignDocChanged()));
			externalChangeDO.setResearchDeptIsDrawingChnagedString(OK_MAP
					.get(externalChangeDO.getResearchDeptIsDrawingChnaged()));

			externalChangeDO.setEngineeringDeptIsBomChangedString(OK_MAP
					.get(externalChangeDO.getEngineeringDeptIsBomChanged()));
			externalChangeDO.setEngineeringDeptIsMoldChangedString(OK_MAP
					.get(externalChangeDO.getEngineeringDeptIsMoldChanged()));
			externalChangeDO.setEngineeringDeptIsProcChangedString(OK_MAP
					.get(externalChangeDO.getEngineeringDeptIsProcChanged()));
			externalChangeDO.setEngineeringDeptIsQcChangedString(OK_MAP
					.get(externalChangeDO.getEngineeringDeptIsQcChanged()));
			externalChangeDO.setEngineeringDeptIsSopChangedString(OK_MAP
					.get(externalChangeDO.getEngineeringDeptIsSopChanged()));

			externalChangeDO.setQualityDeptIsInspectDocChangedString(OK_MAP
					.get(externalChangeDO.getQualityDeptIsInspectDocChanged()));
			externalChangeDO.setQualityDeptIsMarkInFirst3String(OK_MAP
					.get(externalChangeDO.getQualityDeptIsMarkInFirst3()));
			externalChangeDO.setQualityDpetIsTrainedString(OK_MAP
					.get(externalChangeDO.getQualityDpetIsTrained()));
			externalChangeDO.setQualityDpetIsUpgradeInspectionString(OK_MAP
					.get(externalChangeDO.getQualityDpetIsUpgradeInspection()));

			externalChangeDO.setProductDeptIsMaterialCheckedString(OK_MAP
					.get(externalChangeDO.getProductDeptIsMaterialChecked()));
			externalChangeDO.setProductDeptIsRegistedString(OK_MAP
					.get(externalChangeDO.getProductDeptIsRegisted()));
			externalChangeDO.setProductDeptIsTrainedString(OK_MAP
					.get(externalChangeDO.getProductDeptIsTrained()));

			externalChangeDO.setSummaryIsImpactShipMentString(OK_MAP
					.get(externalChangeDO.getSummaryIsImpactShipMent()));
			externalChangeDO
					.setSummaryIsNumberBeforeChangeProdString(OK_MAP
							.get(externalChangeDO
									.getSummaryIsNumberBeforeChangeProd()));
			externalChangeDO.setSummaryIsNumberBeforeChangeSupString(OK_MAP
					.get(externalChangeDO.getSummaryIsNumberBeforeChangeSup()));
			externalChangeDO.setStatusString(EXTERNAL_CHANGE_TICKET_STATUS
					.get(String.valueOf(externalChangeDO.getStatus())));
		}

		return JSONUtils.getPageJSON(list, this.getExternalChangeCount(params));
	}

	@Override
	public String setVerifyTeam(Map<String, String> params) {
		// TODO Auto-generated method stub
		this.getSqlMapClientTemplate().update("externalchange.setVerifyTeam",
				params);
		return "评审组长设置成功";
	}

}
